package com.yjc.hire.server.controller;

import com.yjc.bo.NewArticleBO;
import com.yjc.hire.base.BaseInfoProperties;
import com.yjc.hire.enums.ArticleStatus;
import com.yjc.hire.grace.result.GraceJSONResult;
import com.yjc.hire.grace.result.PageGridResult;
import com.yjc.hire.server.service.ArticleService;
import com.yjc.pojo.Article;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * </p>
 *
 * @author jcyin
 * @since 2025/9/10
 * </p>
 */

@RestController
@RequestMapping("article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("save")
    public GraceJSONResult save(@RequestBody NewArticleBO article) {

        String articleId = article.getArticleId();
        if (StringUtils.isBlank(articleId)) {
            articleService.createArticle(article);
        }else {
            articleService.updateArticle(article);
        }
        return GraceJSONResult.ok();
    }

    @PostMapping("list")
    public GraceJSONResult list(Integer page, Integer limit, ArticleStatus articleStatus){
        PageGridResult<List<Article>> pageList = articleService.list(page,limit,articleStatus);
        return GraceJSONResult.ok(pageList);
    }

    @PostMapping("detail")
    public GraceJSONResult detail(String articleId) {

        return GraceJSONResult.ok();
    }
    @PostMapping("app/read")
    public GraceJSONResult read(String articleId,String userId) {

        String isRead = stringRedisTemplate.opsForValue().get(BaseInfoProperties.REDIS_USER_INFO + ":" + userId
                + ":" + articleId);
        if (StringUtils.isBlank(isRead)) {
            stringRedisTemplate.opsForValue().increment(BaseInfoProperties.REDIS_ARTICLE_READ_COUNTS
                    + ":" + articleId,1);
            //标志以及阅读完成了
            stringRedisTemplate.opsForValue().set(BaseInfoProperties.REDIS_USER_INFO + ":" + userId + ":"
                            + articleId,
                    articleId);
        }
        return GraceJSONResult.ok();
    }

}
